package com.example.todolist;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;
import java.util.Date;

public class DetailActivity extends AppCompatActivity {
    public  static  final  String TAG="detail";
    TextView title;
    TextView detail;
    TextView date;
    TextView ddl;
    Integer id;
    String t;
    String d;
    String UpDateStr;
    String change_date;
    EditText change_ddl;
    String choosedate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        //接受
        Intent intent = getIntent();
        String ItemTitle=intent.getStringExtra("title");
        String ItemDetail=intent.getStringExtra("detail");
        String ItemDate=intent.getStringExtra("date");
        String ItemId=intent.getStringExtra("id");
        id=Integer.parseInt(ItemId);

        Log.i(TAG,"detail:ItemId="+ItemId);
        Log.i(TAG,"detail:ItemTitle="+ItemTitle);
        Log.i(TAG,"detail:ItemDetail="+ItemDetail);

        DBManager dbManager = new DBManager(DetailActivity.this);
        ListItem listItem = new ListItem();
        listItem=dbManager.findById(id);
        String deadline=listItem.getDdl();

        Log.i(TAG,"detail:ddl="+deadline);

        title=findViewById(R.id.detail_title);
        detail=findViewById(R.id.detail_detail);
        date=findViewById(R.id.detail_date);
        ddl=findViewById(R.id.change_date);

        title.setText(ItemTitle);
        detail.setText(ItemDetail);
        date.setText(ItemDate);
        ddl.setText(deadline);

        /*创建点击事件*/
        ddl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar=Calendar.getInstance();
                new DatePickerDialog( DetailActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String text = "你选择了：" + year + "年" + (month + 1) + "月" + dayOfMonth + "日";
                        month=month+1;
                        if(month>9 & dayOfMonth>9) {
                            choosedate = year + "-" +month+"-"+dayOfMonth;
                        }else if(month>9 & dayOfMonth<9){
                            choosedate = year + "-" +month+"-0"+dayOfMonth;
                        }else if(month<9 & dayOfMonth>9){
                            choosedate = year + "-0" +month+"-"+dayOfMonth;
                        }else{
                            choosedate = year + "-0" +month+"-0"+dayOfMonth;
                        }
                        Toast.makeText( DetailActivity.this, text, Toast.LENGTH_SHORT ).show();
                        ddl.setText(choosedate);
                    }
                }
                        ,calendar.get(Calendar.YEAR)
                        ,calendar.get(Calendar.MONTH)
                        ,calendar.get(Calendar.DAY_OF_MONTH)).show();
            }

        });

    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void myclick(View btn){
        //获取用户修改数据
        t=title.getText().toString();
        d=detail.getText().toString();
        UpDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
        change_date = ddl.getText().toString();
        Log.i("run", "修改时间:" + UpDateStr);
        Log.i("run", "修改数据----------" );
        Log.i("run", "修改数据title:"+t );
        Log.i("run", "修改数据detail:"+d );
        Log.i("run", "修改数据id:"+id );


        Log.i("update","_id="+id);
        DBManager dbManager = new DBManager(DetailActivity.this);
        ListItem listItem = new ListItem();
        listItem.setId(id);
        listItem.setTitle(t);
        listItem.setDetail(d);
        listItem.setDate(UpDateStr);
        listItem.setSituation("未完成");
        listItem.setDdl(change_date);

        Log.i("change_date",change_date);
        Log.i("update","赋值");
        dbManager.update(listItem);

        Log.i("update","修改成功");

        Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, MainActivity.class));
        finish();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.check,menu);
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==R.id.detail_check){
            t=title.getText().toString();
            d=detail.getText().toString();
            UpDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
            change_date = ddl.getText().toString();
            Log.i("change_date:",change_date);

            DBManager dbManager = new DBManager(DetailActivity.this);
            ListItem listItem = new ListItem();
            listItem.setId(id);
            listItem.setTitle(t);
            listItem.setDetail(d);
            listItem.setDate(UpDateStr);
            listItem.setSituation("完成");
            listItem.setDdl(change_date);

            Log.i("update","修改状态");
            dbManager.update(listItem);

            Log.i("update","修改成功");

            Toast.makeText(this, "完成任务", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, MainActivity.class));
            finish();

        }

        return super.onOptionsItemSelected(item);
    }

}